/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.idei.filetree;

import java.beans.ExceptionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Emilio
 */
public class FileOrderStatus implements Serializable {
	private static final long serialVersionUID = 23l;
	private DefaultMutableTreeNode treeNodes;
	private String root;
	/**
	 * Mantiene una lista de pares File-DefaultMutableNode.
	 */
	private HashMap<String, DefaultMutableTreeNode> fileNodeMap =
			new HashMap<String, DefaultMutableTreeNode>();

	public FileOrderStatus() {
	}

	private FileOrderStatus(File root) {
		setRoot(root.getAbsolutePath());
		String files[] = new File(getRoot()).list();
		treeNodes = searchNodeForFile(root);
		for (int i = 0; i < files.length; i++) {
			if (acceptedFile(files[i])) {
				treeNodes.add(searchNodeForFile(new File(getRoot(), files[i])));
			}
		}
	}

	public DefaultMutableTreeNode getTreeNodes() {
		return treeNodes;
	}
	
	public void setTreeNodes(DefaultMutableTreeNode treeNode) {
		this.treeNodes = treeNode;
	}

	public boolean acceptedFile(String filename) {
		File f = new File(filename);
		if (f.isHidden()) {
			return false;
		}
		if (filename.startsWith(".")) {
			return false;
		}
		if (f.isDirectory()) {
			return false;
		}
		int i = filename.lastIndexOf('.');
		if (i > 0 && i < filename.length() - 1) {
			return filename.substring(i + 1).toLowerCase().equals("odt");
		}
		return false;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		File rootFile = new File(root);
		if (rootFile.isFile()) {
			throw new IllegalArgumentException("La raíz no puede ser un archivo");
		}
		this.root = root;
	}

	public static FileOrderStatus read(File root) {
		File orderStatusFile = new File(root, "order_status.xml");
		try {
			XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
					new FileInputStream(orderStatusFile)));
			FileOrderStatus result = (FileOrderStatus) decoder.readObject();
			decoder.close();
			return result;
		} catch (FileNotFoundException ex) {
			try {
				FileOrderStatus ret = new FileOrderStatus(root);
				XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
						new FileOutputStream(orderStatusFile)));
				encoder.setExceptionListener(new ExceptionListener() {

					public void exceptionThrown(Exception exception) {
						exception.printStackTrace();
					}
				});
				encoder.writeObject(ret);
				encoder.close();
				return ret;
			} catch (Exception ioe) {
				ioe.printStackTrace();
				throw new IllegalArgumentException("Error fatal al leer estado");
			}
		}
	}

	public void write() {
		File orderStatus = new File(getRoot(), "order_status.xml");
		try {
			XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
					new FileOutputStream(orderStatus)));
			encoder.writeObject(this);
			encoder.close();
		} catch (FileNotFoundException fnfe) {
			throw new IllegalArgumentException("Error fatal al escribir");
		}
	}
	
	public DefaultMutableTreeNode getNodeForFile(String fileID) {
		return getFileNodeMap().get(fileID);
	}
	
	public DefaultMutableTreeNode searchNodeForFile(File file) {
		DefaultMutableTreeNode dmtn;
		String fileID = file.getAbsolutePath();
		if (!fileNodeMap.containsKey(fileID)) {
			dmtn = new DefaultMutableTreeNode(fileID);
			getFileNodeMap().put(fileID, dmtn);
		} else {
			dmtn = getFileNodeMap().get(fileID);
		}
		return dmtn;
	}

	public HashMap<String, DefaultMutableTreeNode> getFileNodeMap() {
		return fileNodeMap;
	}

	public void setFileNodeMap(HashMap<String, DefaultMutableTreeNode> fileNodeMap) {
		this.fileNodeMap = fileNodeMap;
	}
}
